<?php
/********************************************************************
 * project: tardb -- tar file database
 * file: q.php
 * author: xtaci
 * email: xtaci@163.com
 * description:
 *   The query interface for the http server, put this file in a directory
 * where your web server can access, you can query a tile like this:
 *
 *          http://you.ip.address/q.php?v=dir_to_pic
 *   or  
 *          http://you.ip.address/q?v=dir_to_pic  (if url-rewrite is on in .htaccess)
 *
 *   For example:
 *
 *          http://10.0.0.10/image/q.php?v=a/b/c/d/a.jpg
 *   or
 *          http://10.0.0.10/image/q?v=a/b/c/d/a.jpg (if url-rewrite is on in .htaccess)
 *
 *   When a user query request is received by this php, it will first 
 * locate file by 'db' file, and then find the tarball by 'filedb',
 * opens the tarball, seeks to the file position, reads the content
 * and sends out the content from tarball finally.
 * 
 * revision: 1.0
 * date: 2009/12/16
 */

require_once('settings.php');
$param = $_GET["v"];

/*** patches for visiting with row,col,level ***/
if ($param == '')
{
	$row = $_GET["row"];
	$col = $_GET["col"];
	$level = $_GET["level"];

	// --- patch for rowmodel=1
	$rowmodel = $_GET["rowModel"];
	if ($rowmodel == 1)
	{
		$row = pow(2,$level) - 1 - $row;
	}

	$param = xy2abc($row,$col,$level);
}

if (substr($param,0,1)=='/')
	$param = substr($param,1,strlen($param)-1);

if ($base != '' )
    $key = $base.'/'.$param;
else
	$key = $param;

// open index
$db	= dba_open($dbpath,	"r", "db4");
if (!$db) {
	echo "dba_open failed when openning index\n";
	error_exit();
}

// locate index
if (dba_exists($key, $db)) {
    $value = dba_fetch($key, $db);

}
else 
{
	echo 'cannot locate tile - '.$key;
	error_exit();
}

$values = explode('*', $value);
$filehash = $values[0];
$offset = $values[1];
$size = $values[2];

// open filedb
$filedb	= dba_open($filedbpath,	"r", "db4");
if (!$filedb) {
	echo "dba_open failed when openning filedb \n";
	exit;
}

// locate file
if (dba_exists($filehash, $filedb)) {
    $tar_name = dba_fetch($filehash, $filedb);
}
else 
{
	echo 'cannot locate tar file';
	error_exit();
}

//write out picture
$tmp = explode('.', $param);
header('Content-Type: image/'.$tmp[count($tmp) - 1]);
header('Content-Length: '.$size);

// open and write out
$file = @fopen($tar_name, "rb");
@fseek($file,$offset);
$v_block = @fread($file,$size);
echo $v_block;
fclose($file);
dba_close($db);
dba_close($filedb);


function error_exit()
{
	header('HTTP/1.1 404 Not Found');
    header("status: 404 Not Found"); 
	exit;
}


function xy2abc($row,$col,$level)
{
	$x = $row;
	$y = $col;
	$z = $level;

	$ya = 1 << $z;

	// return root;
	if (($y<0) || ($ya-1<$y)) {
		return $GLOBALS['picname'];
	}

	if (($x<0) || ($ya-1<$x)) {
		$x %= $ya;
		if ($x<0) {
			$x += $ya;
		}
	}

	$t='';

	// Create the tile's URL string
	for ($i = $z; $i>0; $i--) {
		$ya /= 2;
		if ($y<$ya) {
			if ($x<$ya) {
				$t = $t."/c";
			} else {
				$t = $t."/a";
				$x -= $ya;
			}
		} else {
			if ($x<$ya) {
				$t = $t."/d";
				$y -= $ya;
			} else {
				$t = $t."/b";
				$x -= $ya;
				$y -= $ya;
			}
		}
	}

	return $t.'/'.$GLOBALS['picname'];
} 
?>
